package com.deng.mapper;

import com.deng.config.AllParamOfUser;
import com.deng.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author 邓小熙
 * @Date 2022/5/2 11:17
 * @Version 1.0
 *        用户mapper类
 */
@Mapper
@Component
public interface UserMapper {
    /**
     *根据昵称查询信息
     */
    @Select("SELECT * FROM social_blog.users WHERE login_name = #{name}")
    User findUserByName(String name);
    /**
     *根据用户ID查询用户
     */
    @Select("SELECT * FROM social_blog.users WHERE id = #{id};")
    User findUserById(int id);
    /**
     * 个人简介，通过名字查找用户，根据昵称查询基本信息
     * @param userName
     * @return
     */
    @Select("SELECT * FROM social_blog.users WHERE login_name=#{name} ")
    User findUsersByName(String userName);
    /**
     * 根据name查询paper表的author信息
     * @param userName
     * @return
     */
    @Select("SELECT u.id FROM social_blog.users u WHERE u.login_name=#{name}")
    int findAuthorByName(String userName);
    /**
     * 通过ID找作者
     * @param id
     * @return
     */
    @Select("SELECT u.login_name FROM social_blog.users u WHERE u.id=#{id}")
    String findUserNameById(int id);
    /**
     * 改变用户详情修改
     * @param allParamOfUser
     * @return
     */
    @Update(" UPDATE social_blog.users u,social_blog.user_role_permission  urp SET u.status=#{allParamOfUser.user.status}, urp.role_id=(SELECT r.id from social_blog.`role` r WHERE r.role_name=#{allParamOfUser.review}) WHERE u.id=urp.user_id AND u.id=#{allParamOfUser.userId}")
    AllParamOfUser edictDetail(@Param("allParamOfUser") AllParamOfUser allParamOfUser);
    /**
     * 改变用户详情修改
     * @param users
     * @param name
     * @return
     */
    @Update("UPDATE social_blog.users SET head=#{users.head} , real_name = #{users.real_name},email=#{users.email} WHERE login_name=#{name}")
    int updateUserMes(@Param("users") User users,@Param("name") String name);
    /**
     * 人员状态修改
     * @param review
     * @param status
     * @param userId
     * @return
     */
    @Update(" UPDATE social_blog.users u,social_blog.user_role_permission  urp SET u.status=#{status}, urp.role_id=(SELECT r.id from social_blog.`role` r WHERE r.role_name=#{review}) WHERE u.id=urp.user_id AND u.id=#{userId}")
    int updateUserStatus(@Param("review") String review,@Param("status") int status,@Param("userId") int userId);

    /**
     * 分页查询所有用户
     * @param page
     * @param limit
     * @return
     */
    @Select({
            "<script>",
            "SELECT * FROM social_blog.users",
            "ORDER BY id ASC",
            " LIMIT #{page},#{limit}",
            "</script>"
    })
    List<User> findAllUsersListLimit(@Param("page") int page,@Param("limit")  int limit );
    /**
     * 查询所有用户
     * @return
     */
    @Select({
            "<script>",
            "SELECT * FROM social_blog.users",
            "ORDER BY id ASC",
            "</script>"
    })
    List<User> findAllUsersList();


    /**
     * 改密码
     * @param userName 用户名
     * @param newPassWord 新密码
     * @param oldPassWord 旧密码
     * @return 1
     */
    @Update("UPDATE social_blog.users SET password = #{newPassWord} WHERE login_name = #{userName} AND password = #{oldPassWord}")
    int updateUserPass(@Param("userName") String userName,@Param("newPassWord") String newPassWord,@Param("oldPassWord") String oldPassWord);
}
